package com.mastercard.assets.web.struts.actions;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mastercard.assets.jpa.TblAlerta;
import com.mastercard.assets.jpa.TblAsset;
import com.mastercard.assets.jpa.TblCampania;
import com.mastercard.assets.jpa.TblCategoria;
import com.mastercard.assets.jpa.TblCuentaPaquete;
import com.mastercard.assets.jpa.TblFlujo;
import com.mastercard.assets.jpa.TblForma;
import com.mastercard.assets.jpa.TblFormaAlarma;
import com.mastercard.assets.jpa.TblFormaAlerta;
import com.mastercard.assets.jpa.TblFormaSalida;
import com.mastercard.assets.jpa.TblNivel;
import com.mastercard.assets.jpa.TblPaquete;
import com.mastercard.assets.jpa.TblRole;
import com.mastercard.assets.jpa.TblSubcateg;
import com.mastercard.assets.jpa.TblTarea;
import com.mastercard.assets.jpa.TblTipoCampania;
import com.mastercard.assets.jpa.TblTipoCuenta;
import com.mastercard.assets.util.MCUtil;
import com.mastercard.assets.util.dao.MasterCardDAO;
import com.mastercard.assets.web.struts.forms.AlertasEditarForm;
import com.mastercard.assets.web.struts.forms.AlertasForm;
import com.mastercard.assets.web.struts.forms.AssetAltaForm;
import com.mastercard.assets.web.struts.forms.AssetAsignarPaquetesForm;
import com.mastercard.assets.web.struts.forms.AssetBorrarForm;
import com.mastercard.assets.web.struts.forms.AssetBuscarForm;
import com.mastercard.assets.web.struts.forms.AssetEditarForm;
import com.mastercard.assets.web.struts.forms.AssetModificarPaquetesForm;
import com.mastercard.assets.web.struts.forms.CampanasPorAutorizarForm;
import com.mastercard.assets.web.struts.forms.FlujosFEditarForm;
import com.mastercard.assets.web.struts.forms.FlujosFForm;
import com.mastercard.assets.web.struts.forms.FormasAgregarForm;
import com.mastercard.assets.web.struts.forms.FormasEditarForm;
import com.mastercard.assets.web.struts.forms.FormasForm;
import com.mastercard.assets.web.struts.forms.GenerarCampanaForm;
import com.mastercard.assets.web.struts.forms.GenerarCampanaRegistrarForm;
import com.mastercard.assets.web.struts.forms.LoginForm;
import com.mastercard.assets.web.struts.forms.TareasForm;
import com.mastercard.assets.web.struts.forms.TareasOperationForm;
import com.mastercard.assets.web.util.to.FormaSalidaManager;
import com.mastercard.assets.web.util.to.ManejadorPaquetesAsignados;
import com.mastercard.assets.web.util.to.PaqueteAutorizado;
import com.mastercard.assets.web.util.to.UserLogin;

public class TareasOperationIn
	extends
		Action
{
	public ActionForward execute(
							ActionMapping mapping , 
							ActionForm form ,
							HttpServletRequest request , 
							HttpServletResponse response )
		throws 
			IOException , 
			ServletException
	{
		HttpSession session = request.getSession( ) ;
		
		// en caos de null
		if( session == null )
			return mapping.findForward( "noLogin" ) ;
		
		// toma el userLogin
		UserLogin user = ( UserLogin ) session.getAttribute( "UserLogin" ) ;
		
		// en caso de null
		if( user == null )
			return mapping.findForward( "noLogin" ) ;
		
		int role = user.getActualRoleId( ) ;
		
		if( role != 2 )
			return mapping.findForward( "RoleInvalid" ) ;
		
		// toma el form
		TareasOperationForm f = ( TareasOperationForm ) form ;
                int idioma = user.getIdioma( ) ;
		
		// en caso de null
		if( f == null )
			return mapping.findForward( "SourceInvalid" ) ;
		
		// toma los datos
		int tipoAlerta = f.getTipoAlerta( ) ;
		String email = f.getEmail( ) ;
		int cveNivel = f.getCveNivel( ) ;
		int cveRole = f.getCveRole( ) ;
		int cveTarea = f.getCveTarea( ) ;
		int cveFlujo = f.getCveFlujo( ) ;
		int cveAlerta = f.getCveAlerta( ) ;
		int cveAlarma = f.getCveAlarma( ) ;
		int cveSalida = f.getCveSalida( ) ;
		int operation = f.getOperation( ) ;
		int cargar = f.getCargar( ) ;
		int cveFormaAlerta = f.getCveFormaAlerta( ) ;
		
		TblFormaSalida salida = null ;
		TblTarea tarea = null ;
		TareasOperationForm tareasOperationForm = null ;
		TblNivel nivel = null ;
		String fecha = null ;
		List< TblAlerta > alertas = null ;
		List< TblRole > roles = null ;
		List< TblForma > formas = null ;
		List< TblNivel > niveles = null ;
		TblFormaAlerta formaAlerta = null ;
		TblFormaAlarma formaAlarma = null ;
		List< TblFormaAlarma > alarmas = null ;
		TblFlujo flujo = null ;
		List< TblTarea > tareas = null ;
		
		// revisa operacion
		switch( operation )
		{
			// en caso de agregar forma
			case 1 :
				
				// carga los roles
				roles = MasterCardDAO.getAllActiveRoles( ) ;
				request.setAttribute( "Roles" , roles ) ;
				
				// carga las formas
				formas = MasterCardDAO.getAllActiveFormas( ) ;
				request.setAttribute( "Formas" , formas ) ;
				
				// en caso de que venga algun role
				if( cveRole > 0 )
				{
					// carga los roles
					niveles = MasterCardDAO.getAllActiveLevelsFromRole( cveRole ) ;
					request.setAttribute( "Niveles" , niveles ) ;
				}
				
				flujo = MasterCardDAO.findFlujoById( cveFlujo ) ;
				f.setNombreFlujo( flujo.getNombre( ) ) ;
				
				request.setAttribute( "TareasOperationForm" , f ) ;
				
                                MasterCardDAO.closeEntityManager( ) ;
                                if( idioma == 0 ) {
                                    return mapping.findForward( "AddTarea" ) ;
                                } else 
                                    return mapping.findForward( "AddTareaEng" ) ;
				
			// en caso de eliminar la tarea
			case 2 :
				
				// levanta la forma
				tarea = MasterCardDAO.findWorkById( cveTarea ) ;
				
				FlujosFForm flujosFForm = new FlujosFForm( ) ;
				
				request.setAttribute( "Tarea" , tarea ) ;
				
                                MasterCardDAO.closeEntityManager( ) ;
                                if( idioma == 0 ) {
                                    return mapping.findForward( "RemoveTarea" ) ;
                                } else 
                                    return mapping.findForward( "RemoveTareaEng" ) ;
				
				
//			// en caso de editar la tarea
			case 3 :
				
				// levanta la forma
				tarea = MasterCardDAO.findWorkById( cveTarea ) ;
				
				// en caso de null
				if( tarea == null )
				{
					
				}
				
//				// lo coloca en el form adecuado
				tareasOperationForm = new TareasOperationForm( ) ;
				
				tareasOperationForm.setCveFlujo( tarea.getTblFlujo( ).getCveFlujo( ) ) ;
				tareasOperationForm.setNombre( tarea.getNombre( ) ) ;
				tareasOperationForm.setDescripcion( tarea.getDescripcion( ) ) ;
				if( cveRole > 0 && cveRole != tarea.getTblRole( ).getCveRole( ) )
					tareasOperationForm.setCveRole( cveRole ) ;
				else
					tareasOperationForm.setCveRole( tarea.getTblRole( ).getCveRole( ) ) ;
				nivel = tarea.getTblNivel( ) ;
				tareasOperationForm.setCveNivel( ( nivel == null ) ? 0 : nivel.getCveNivel( ) ) ;
				tareasOperationForm.setCveForma( tarea.getTblForma( ).getCveForma( ) ) ;
				fecha = MCUtil.formatTimestamp( tarea.getFechaActiva( ) , "dd/MM/yyyy" ) ;
				tareasOperationForm.setActiveDate( fecha ) ;
				tareasOperationForm.setFechaActivacionDias( ( ( tarea.getFecActDias( ) == 0 ) ? "" : tarea.getFecActDias( ) + "" ) ) ;
				tareasOperationForm.setFechaActivacionDate( tarea.getFecActFecha( ) ) ;
				fecha = MCUtil.formatTimestamp( tarea.getFechaLimite( ) , "dd/MM/yyyy" ) ;
				tareasOperationForm.setLimitDate( fecha ) ;
				tareasOperationForm.setFechaLimiteDias( ( ( tarea.getFecLimDias( ) == 0 ) ? "" : tarea.getFecLimDias( ) + "" ) ) ;
				tareasOperationForm.setFechaLimiteDate( tarea.getFecLimFechas( ) ) ;
				tareasOperationForm.setCveTarea( tarea.getCveTarea( ) ) ;
				tareasOperationForm.setNombreFlujo( tarea.getTblFlujo( ).getNombre( ) ) ;
                                tareasOperationForm.setMultiple( tarea.getMultiple( ) ) ;
                                tareasOperationForm.setMicroFlujo( tarea.getMicroFlujo( ) ) ;
				
				request.setAttribute( "TareasOperationForm" , tareasOperationForm ) ;
				
				// carga los roles
				List< TblRole > rolesA = MasterCardDAO.getAllActiveRoles( ) ;
				request.setAttribute( "Roles" , rolesA ) ;
				
				// carga las formas
				List< TblForma > formasA = MasterCardDAO.getAllActiveFormas( ) ;
				request.setAttribute( "Formas" , formasA ) ;
				
				int cveRoleA = tareasOperationForm.getCveRole( ) ;
				
				// en caso de que venga algun role
				if( cveRoleA > 0 )
				{
					// carga los roles
					List< TblNivel > nivelesA = MasterCardDAO.getAllActiveLevelsFromRole( cveRoleA ) ;
					request.setAttribute( "Niveles" , nivelesA ) ;
				}
				
                                MasterCardDAO.closeEntityManager( ) ;
                                if( idioma == 0 ) {
                                    return mapping.findForward( "EditTarea" ) ;
                                } else 
                                    return mapping.findForward( "EditTareaEng" ) ;
				
				
			// en caso de que quiera editar las salidas
			case 4 :
				
				// levanta la forma
				tarea = MasterCardDAO.findWorkById( cveTarea ) ;
				
				// en caso de null
				if( tarea == null )
				{
					
				}
				
//				// lo coloca en el form adecuado
				tareasOperationForm = new TareasOperationForm( ) ;
				
				tareasOperationForm.setCveFlujo( tarea.getTblFlujo( ).getCveFlujo( ) ) ;
				tareasOperationForm.setCveTarea( tarea.getCveTarea( ) ) ;
				
				request.setAttribute( "TareasOperationForm" , tareasOperationForm ) ;
				
				// busca las formas
				List< TblTarea > formasExistentes = MasterCardDAO.getAllActiveFormasInFlow( cveFlujo ) ;
				
				// en caso deq ue no haya formas
				if( formasExistentes == null || formasExistentes.size( ) == 0 )
				{
					user.setMessageError( MCUtil.messages.getString( "com.mastercard.assets.web.modules.tareas.add.errors.ThereIsNoForms" , tarea.getTblFlujo( ).getNombre( ) ) ) ;
					
					user.setNextWorkArea( "/TareasIndexIn.do?operation=" + 4 + "&cveFlujo=" + cveFlujo ) ;
					
                                        MasterCardDAO.closeEntityManager( ) ;
					return mapping.findForward( "success" ) ;
				}
				
				FormaSalidaManager salidasManager = new FormaSalidaManager( tarea , formasExistentes ) ;
				
				salidasManager.initialize( ) ;
				
				tareasOperationForm.setSalidasManager( salidasManager ) ;
				
				// guarda la tarea
				request.setAttribute( "Tarea" , tarea ) ;
				
                                MasterCardDAO.closeEntityManager( ) ;
                                if( idioma == 0 ) {
                                    return mapping.findForward( "EditTareaSalidas" ) ;
                                } else 
                                    return mapping.findForward( "EditTareaSalidasEng" ) ;
				
				
			// en caso de que quiera editar las alertas de cada salida de la tarea del flujo
			case 5 :
				
				// levanta la forma
				tarea = MasterCardDAO.findWorkById( cveTarea ) ;
				
				// en caso de null
				if( tarea == null )
				{
					
				}
				
				// guarda la tarea
				request.setAttribute( "Tarea" , tarea ) ;
				
				// calcula la salida
				salida = MasterCardDAO.findExitById( cveSalida ) ;
				
				if( salida == null )
				{
					
				}
				
				request.setAttribute( "Salida" , salida ) ;
				
				tareasOperationForm = new TareasOperationForm( ) ;
				
				tareasOperationForm.setCveFlujo( cveFlujo ) ;
				tareasOperationForm.setCveTarea( cveTarea ) ;
				tareasOperationForm.setCveSalida( cveSalida ) ;
//				tareasOperationForm.setOperation( 4 ) ;
				flujo = MasterCardDAO.findFlujoById( cveFlujo ) ;
				tareasOperationForm.setNombreFlujo( flujo.getNombre( ) ) ;
				
				
				request.setAttribute( "TareasOperationForm" , tareasOperationForm ) ;
				
                                MasterCardDAO.closeEntityManager( ) ;
				return mapping.findForward( "EditTareaSalidasAlertas" ) ;
				
			// en caso de agregar una alerta a la salida de la tarea
			case 6 :
				
				System.out.println( "Entra a cargar datos para agregar alerta: " + cveRole + " - " + cveFlujo + " - " + cveTarea + " - " + cveSalida ) ;
				
				// carga los roles
				roles = MasterCardDAO.getAllActiveRoles( ) ;
				request.setAttribute( "Roles" , roles ) ;
				
				// carga las alertas
				alertas = MasterCardDAO.getAllActiveAlertas( ) ;
				request.setAttribute( "Alertas" , alertas ) ;
				
				// levanta la tarea
				tarea = MasterCardDAO.findWorkById( cveTarea ) ;
				
				// en caso de null
				if( tarea == null )
				{
					
				}
				
				// guarda la tarea
				request.setAttribute( "Tarea" , tarea ) ;
				
				// calcula la salida
				salida = MasterCardDAO.findExitById( cveSalida ) ;
				
				if( salida == null )
				{
					
				}
				
				// guarda la salida
				request.setAttribute( "Salida" , salida ) ;
				
				tareasOperationForm = new TareasOperationForm( ) ;
				
				tareasOperationForm.setCveFlujo( cveFlujo ) ;
				tareasOperationForm.setCveTarea( cveTarea ) ;
				tareasOperationForm.setCveSalida( cveSalida ) ;
				tareasOperationForm.setCveRole( cveRole ) ;
				
				// en caso de que venga algun role
				if( cveRole > 0 )
				{
					// carga los roles
					niveles = MasterCardDAO.getAllActiveLevelsFromRole( cveRole ) ;
					request.setAttribute( "Niveles" , niveles ) ;
				}
				
				// carga el form
//				request.setAttribute( "TareasOperationForm" , tareasOperationForm ) ;
				
                                MasterCardDAO.closeEntityManager( ) ;
				return mapping.findForward( "AddTareaSalidasAlertas" ) ;
				
			// en caso de que desee borrar la formaAlerta
			case 7 :
				
				try
				{
					MasterCardDAO.eraseAlertInExitWork( cveFormaAlerta ) ;
				}catch( Exception e )
				{
                                    MasterCardDAO.closeEntityManager( ) ;
					user.setMessageError( e.getMessage( ) ) ;
				}
				
				// levanta la forma
				tarea = MasterCardDAO.findWorkById( cveTarea ) ;
				
				// en caso de null
				if( tarea == null )
				{
					
				}
				
				// guarda la tarea
				request.setAttribute( "Tarea" , tarea ) ;
				
				// calcula la salida
				salida = MasterCardDAO.findExitById( cveSalida ) ;
				
				if( salida == null )
				{
					
				}
				
				request.setAttribute( "Salida" , salida ) ;
				
				tareasOperationForm = new TareasOperationForm( ) ;
				
				tareasOperationForm.setCveFlujo( cveFlujo ) ;
				tareasOperationForm.setCveTarea( cveTarea ) ;
				tareasOperationForm.setCveSalida( cveSalida ) ;
//				tareasOperationForm.setOperation( 4 ) ;
				
				request.setAttribute( "TareasOperationForm" , tareasOperationForm ) ;
				
                                MasterCardDAO.closeEntityManager( ) ;
				return mapping.findForward( "EditTareaSalidasAlertas" ) ;
				
			// en caso de que el usuario quiera editar la forma alerta
			case 8:
				
				// toma la formaalerta
				formaAlerta = MasterCardDAO.findFormAlertById( cveFormaAlerta ) ;
				
				request.setAttribute( "FormaAlerta" , formaAlerta ) ;
				
				tareasOperationForm = new TareasOperationForm( ) ;
				
				System.out.println( "Cargar: " + cargar ) ;
				
				tareasOperationForm.setCveFlujo( cveFlujo ) ;
				tareasOperationForm.setCveTarea( cveTarea ) ;
				tareasOperationForm.setCveSalida( cveSalida ) ;
				tareasOperationForm.setCveFormaAlerta( cveFormaAlerta ) ;
				if( cargar == 1 )
				{
					tareasOperationForm.setCveAlerta( cveAlerta ) ;
					tareasOperationForm.setCveRole( cveRole ) ;
					tareasOperationForm.setTipoAlerta( tipoAlerta ) ;
					tareasOperationForm.setEmail( email ) ;
					tareasOperationForm.setCveNivel( cveNivel ) ;
				}
				else
				{
					tareasOperationForm.setCveAlerta( formaAlerta.getTblAlerta( ).getCveAlerta( ) ) ;
					tareasOperationForm.setCveRole( ( f.getCargar( ) == 1 ) ? cveRole : formaAlerta.getTblRole( ).getCveRole( ) ) ;
					tareasOperationForm.setTipoAlerta( formaAlerta.getTipo( ) ) ;
					tareasOperationForm.setEmail( formaAlerta.getEmail( ) ) ;
					tareasOperationForm.setCveNivel( ( ( formaAlerta.getTblNivel( ) == null ) ? 0 : formaAlerta.getTblNivel( ).getCveNivel( ) ) ) ;
				}
//				tareasOperationForm.setOperation( 4 ) ;
				
				request.setAttribute( "TareasOperationForm" , tareasOperationForm ) ;
				
				// carga los roles
				roles = MasterCardDAO.getAllActiveRoles( ) ;
				request.setAttribute( "Roles" , roles ) ;
				
				// carga las alertas
				alertas = MasterCardDAO.getAllActiveAlertas( ) ;
				request.setAttribute( "Alertas" , alertas ) ;
				
				// levanta la tarea
				tarea = MasterCardDAO.findWorkById( cveTarea ) ;
				
				// en caso de null
				if( tarea == null )
				{
					
				}
				
				// guarda la tarea
				request.setAttribute( "Tarea" , tarea ) ;
				
				// calcula la salida
				salida = MasterCardDAO.findExitById( cveSalida ) ;
				
				if( salida == null )
				{
					
				}
				
				// guarda la salida
				request.setAttribute( "Salida" , salida ) ;
				
				// en caso de que venga algun role
				if( cveRole > 0 )
				{
					// carga los roles
					niveles = MasterCardDAO.getAllActiveLevelsFromRole( cveRole ) ;
					request.setAttribute( "Niveles" , niveles ) ;
				}
				else if( tareasOperationForm.getCveRole( ) > 0 )
				{
					// carga los roles
					niveles = MasterCardDAO.getAllActiveLevelsFromRole( tareasOperationForm.getCveRole( ) ) ;
					request.setAttribute( "Niveles" , niveles ) ;
				}
				
                                MasterCardDAO.closeEntityManager( ) ;
				return mapping.findForward( "EditTareaSalidasAlertasForm" ) ;
				
			// En caso de que quiera ir al listado de alarmas de la tarea
			case 9:
				
				// levanta la tarea
				tarea = MasterCardDAO.findWorkById( cveTarea ) ;
				
				// en caso de null
				if( tarea == null )
				{
					
				}
				
				// guarda la tarea
				request.setAttribute( "Tarea" , tarea ) ;
				
				// revisa las alarmas
				alarmas = MasterCardDAO.getAllActiveAlarmsInWork( cveTarea ) ;
				
				// guarda
				request.setAttribute( "Alarmas" , alarmas ) ;
				
				tareasOperationForm = new TareasOperationForm( ) ;
				
				tareasOperationForm.setCveFlujo( cveFlujo ) ;
				tareasOperationForm.setCveTarea( cveTarea ) ;
				
				request.setAttribute( "TareasOperationForm" , tareasOperationForm ) ;
				
                                MasterCardDAO.closeEntityManager( ) ;
				return mapping.findForward( "ListWorkAlarms" ) ;
				
			// en caso de querer agregar una alarma a la tarea
			case 10 :
				
				// carga los roles
				roles = MasterCardDAO.getAllActiveRoles( ) ;
				request.setAttribute( "Roles" , roles ) ;
				
				// carga las alertas
				alertas = MasterCardDAO.getAllActiveAlertas( ) ;
				request.setAttribute( "Alertas" , alertas ) ;
				
				// levanta la tarea
				tarea = MasterCardDAO.findWorkById( cveTarea ) ;
				
				// en caso de null
				if( tarea == null )
				{
					
				}
				
				// guarda la tarea
				request.setAttribute( "Tarea" , tarea ) ;
				
				// calcula la salida
				salida = MasterCardDAO.findExitById( cveSalida ) ;
				
				if( salida == null )
				{
					
				}
				
				// guarda la salida
				request.setAttribute( "Salida" , salida ) ;
				
				tareasOperationForm = new TareasOperationForm( ) ;
				
				tareasOperationForm.setCveFlujo( cveFlujo ) ;
				tareasOperationForm.setCveTarea( cveTarea ) ;
				tareasOperationForm.setCveSalida( cveSalida ) ;
				tareasOperationForm.setCveRole( cveRole ) ;
				
				// en caso de que venga algun role
				if( cveRole > 0 )
				{
					// carga los roles
					niveles = MasterCardDAO.getAllActiveLevelsFromRole( cveRole ) ;
					request.setAttribute( "Niveles" , niveles ) ;
				}
				
				// carga el form
//				request.setAttribute( "TareasOperationForm" , tareasOperationForm ) ;
				
                                MasterCardDAO.closeEntityManager( ) ;
				return mapping.findForward( "AddAlarmToWork" ) ;
				
			//en caso d que quiera cargar los niveles en la pantalla de agregar alarma
			case 11 :
				
				// carga los roles
				roles = MasterCardDAO.getAllActiveRoles( ) ;
				request.setAttribute( "Roles" , roles ) ;
				
				// carga las alertas
				alertas = MasterCardDAO.getAllActiveAlertas( ) ;
				request.setAttribute( "Alertas" , alertas ) ;
				
				// levanta la tarea
				tarea = MasterCardDAO.findWorkById( cveTarea ) ;
				
				// en caso de null
				if( tarea == null )
				{
					
				}
				
				// guarda la tarea
				request.setAttribute( "Tarea" , tarea ) ;
				
				// calcula la salida
				salida = MasterCardDAO.findExitById( cveSalida ) ;
				
				if( salida == null )
				{
					
				}
				
				// guarda la salida
				request.setAttribute( "Salida" , salida ) ;
				
				tareasOperationForm = new TareasOperationForm( ) ;
				
				System.out.println( "Cargar: " + cargar ) ;
				
				tareasOperationForm.setCveFlujo( cveFlujo ) ;
				tareasOperationForm.setCveTarea( cveTarea ) ;
				tareasOperationForm.setCveSalida( cveSalida ) ;
				tareasOperationForm.setCveFormaAlerta( cveFormaAlerta ) ;
				if( cargar == 1 )
				{
					tareasOperationForm.setCveAlerta( cveAlerta ) ;
					tareasOperationForm.setCveRole( cveRole ) ;
					tareasOperationForm.setTipoAlerta( tipoAlerta ) ;
					tareasOperationForm.setEmail( email ) ;
					tareasOperationForm.setCveNivel( cveNivel ) ;
				}
				else
				{
					tareasOperationForm.setCveAlerta( formaAlerta.getTblAlerta( ).getCveAlerta( ) ) ;
					tareasOperationForm.setCveRole( ( f.getCargar( ) == 1 ) ? cveRole : formaAlerta.getTblRole( ).getCveRole( ) ) ;
					tareasOperationForm.setTipoAlerta( formaAlerta.getTipo( ) ) ;
					tareasOperationForm.setEmail( formaAlerta.getEmail( ) ) ;
					tareasOperationForm.setCveNivel( ( ( formaAlerta.getTblNivel( ) == null ) ? 0 : formaAlerta.getTblNivel( ).getCveNivel( ) ) ) ;
				}
				
				// en caso de que venga algun role
				if( cveRole > 0 )
				{
					// carga los roles
					niveles = MasterCardDAO.getAllActiveLevelsFromRole( cveRole ) ;
					request.setAttribute( "Niveles" , niveles ) ;
				}
				else if( tareasOperationForm.getCveRole( ) > 0 )
				{
					// carga los roles
					niveles = MasterCardDAO.getAllActiveLevelsFromRole( tareasOperationForm.getCveRole( ) ) ;
					request.setAttribute( "Niveles" , niveles ) ;
				}
				
				// carga el form
//				request.setAttribute( "TareasOperationForm" , tareasOperationForm ) ;
				
                                MasterCardDAO.closeEntityManager( ) ;
				return mapping.findForward( "AddAlarmToWork" ) ;
				
				
			// en caso de que quiera borrar la alarma
			case 12 :
				
				// levanta la tarea
				try
				{
					
					// levanta la forma
					formaAlarma = MasterCardDAO.findAlarmById( cveAlarma ) ;
					
					// guarda
					request.setAttribute( "Alarma" , formaAlarma ) ;
					
					// levanta la forma
					tarea = MasterCardDAO.findWorkById( cveTarea ) ;
					
					request.setAttribute( "Tarea" , tarea ) ;
					
					tareasOperationForm = new TareasOperationForm( ) ;
					
					tareasOperationForm.setCveFlujo( cveFlujo ) ;
					tareasOperationForm.setCveTarea( cveTarea ) ;
					
					request.setAttribute( "TareasOperationForm" , tareasOperationForm ) ;
					
					return mapping.findForward( "RemoveAlarm" ) ;
				}catch( Exception e )
				{
					MasterCardDAO.closeEntityManager( ) ;
					// levanta la tarea
					tarea = MasterCardDAO.findWorkById( cveTarea ) ;
					
					// en caso de null
					if( tarea == null )
					{
						
					}
					
					// guarda la tarea
					request.setAttribute( "Tarea" , tarea ) ;
					
					// revisa las alarmas
					alarmas = MasterCardDAO.getAllActiveAlarmsInWork( cveTarea ) ;
					
					// guarda
					request.setAttribute( "Alarmas" , alarmas ) ;
					
					tareasOperationForm = new TareasOperationForm( ) ;
					
					tareasOperationForm.setCveFlujo( cveFlujo ) ;
					tareasOperationForm.setCveTarea( cveTarea ) ;
					
					request.setAttribute( "TareasOperationForm" , tareasOperationForm ) ;
					
					return mapping.findForward( "ListWorkAlarms" ) ;
					
				}
				
			// en caso d eque quiera ir a la edicion de una alarma
			case 13 :
				
				// levanta la forma
				formaAlarma = MasterCardDAO.findAlarmById( cveAlarma ) ;
				
				// guarda
				request.setAttribute( "Alarma" , formaAlarma ) ;
				
				// carga los roles
				roles = MasterCardDAO.getAllActiveRoles( ) ;
				request.setAttribute( "Roles" , roles ) ;
				
				// carga las alertas
				alertas = MasterCardDAO.getAllActiveAlertas( ) ;
				request.setAttribute( "Alertas" , alertas ) ;
				
				// levanta la tarea
				tarea = MasterCardDAO.findWorkById( cveTarea ) ;
				
				// en caso de null
				if( tarea == null )
				{
					
				}
				
				// guarda la tarea
				request.setAttribute( "Tarea" , tarea ) ;
				
				tareasOperationForm = new TareasOperationForm( ) ;
				
				tareasOperationForm.setCveAlarma( formaAlarma.getCveFormaAlarma( ) ) ;
				tareasOperationForm.setCveFlujo( cveFlujo ) ;
				tareasOperationForm.setCveTarea( cveTarea ) ;
				tareasOperationForm.setCveAlerta( formaAlarma.getTblAlerta( ).getCveAlerta( ) ) ;
				tareasOperationForm.setCveRole( formaAlarma.getTblRole( ).getCveRole( ) ) ;
				tareasOperationForm.setCveNivel( ( ( formaAlarma.getTblNivel( ) == null ) ? 0 : formaAlarma.getTblNivel( ).getCveNivel( ) ) ) ;
				tareasOperationForm.setTipoAlerta( formaAlarma.getTipo( ) ) ;
				tareasOperationForm.setEmail( formaAlarma.getEmail( ) ) ;
				tareasOperationForm.setTipoAlarma( formaAlarma.getTipoAlarma( ) ) ;
				tareasOperationForm.setDias( "" + formaAlarma.getDiasAlarma( ) ) ;
				tareasOperationForm.setHoras( formaAlarma.getHorasAlarma( ) ) ;
				
				// en caso de que venga algun role
				if( cveRole > 0 )
				{
					// carga los roles
					niveles = MasterCardDAO.getAllActiveLevelsFromRole( cveRole ) ;
					request.setAttribute( "Niveles" , niveles ) ;
				}
				else if( tareasOperationForm.getCveRole( ) > 0 )
				{
					// carga los roles
					niveles = MasterCardDAO.getAllActiveLevelsFromRole( tareasOperationForm.getCveRole( ) ) ;
					request.setAttribute( "Niveles" , niveles ) ;
				}
				
				// carga el form
				request.setAttribute( "TareasOperationForm" , tareasOperationForm ) ;
				
                                MasterCardDAO.closeEntityManager( ) ;
				return mapping.findForward( "EditAlarmToWork" ) ;
				
				
			//en caso d que quiera cargar los niveles en la pantalla de editar alarma
			case 14 :
				
				// levanta la forma
				formaAlarma = MasterCardDAO.findAlarmById( cveAlarma ) ;
				
				// guarda
				request.setAttribute( "Alarma" , formaAlarma ) ;
				
				// carga los roles
				roles = MasterCardDAO.getAllActiveRoles( ) ;
				request.setAttribute( "Roles" , roles ) ;
				
				// carga las alertas
				alertas = MasterCardDAO.getAllActiveAlertas( ) ;
				request.setAttribute( "Alertas" , alertas ) ;
				
				// levanta la tarea
				tarea = MasterCardDAO.findWorkById( cveTarea ) ;
				
				// en caso de null
				if( tarea == null )
				{
					
				}
				
				// guarda la tarea
				request.setAttribute( "Tarea" , tarea ) ;
				
				// calcula la salida
				salida = MasterCardDAO.findExitById( cveSalida ) ;
				
				if( salida == null )
				{
					
				}
				
				// guarda la salida
				request.setAttribute( "Salida" , salida ) ;
				
				tareasOperationForm = new TareasOperationForm( ) ;
				
				System.out.println( "Cargar: " + cargar ) ;
				
				tareasOperationForm.setCveFlujo( cveFlujo ) ;
				tareasOperationForm.setCveTarea( cveTarea ) ;
				tareasOperationForm.setCveSalida( cveSalida ) ;
				tareasOperationForm.setCveAlarma( formaAlarma.getCveFormaAlarma( ) ) ;
				tareasOperationForm.setCveFormaAlerta( cveFormaAlerta ) ;
				if( cargar == 1 )
				{
					tareasOperationForm.setCveAlerta( cveAlerta ) ;
					tareasOperationForm.setCveRole( cveRole ) ;
					tareasOperationForm.setTipoAlerta( tipoAlerta ) ;
					tareasOperationForm.setEmail( email ) ;
					tareasOperationForm.setCveNivel( cveNivel ) ;
				}
				else
				{
					tareasOperationForm.setCveAlerta( formaAlerta.getTblAlerta( ).getCveAlerta( ) ) ;
					tareasOperationForm.setCveRole( ( f.getCargar( ) == 1 ) ? cveRole : formaAlerta.getTblRole( ).getCveRole( ) ) ;
					tareasOperationForm.setTipoAlerta( formaAlerta.getTipo( ) ) ;
					tareasOperationForm.setEmail( formaAlerta.getEmail( ) ) ;
					tareasOperationForm.setCveNivel( ( ( formaAlerta.getTblNivel( ) == null ) ? 0 : formaAlerta.getTblNivel( ).getCveNivel( ) ) ) ;
				}
				tareasOperationForm.setDias( "" + formaAlarma.getDiasAlarma( ) ) ;
				tareasOperationForm.setHoras( formaAlarma.getHorasAlarma( ) ) ;
				
				// en caso de que venga algun role
				if( cveRole > 0 )
				{
					// carga los roles
					niveles = MasterCardDAO.getAllActiveLevelsFromRole( cveRole ) ;
					request.setAttribute( "Niveles" , niveles ) ;
				}
				else if( tareasOperationForm.getCveRole( ) > 0 )
				{
					// carga los roles
					niveles = MasterCardDAO.getAllActiveLevelsFromRole( tareasOperationForm.getCveRole( ) ) ;
					request.setAttribute( "Niveles" , niveles ) ;
				}
				
				// carga el form
//					request.setAttribute( "TareasOperationForm" , tareasOperationForm ) ;
				
                                MasterCardDAO.closeEntityManager( ) ;
				return mapping.findForward( "EditAlarmToWork" ) ;
				
			case 15 :
				
				System.out.println( "TareasOperationIn: 15" ) ;
				
				try
				{
					MasterCardDAO.setFirstWork( cveTarea ) ;
				}catch( Exception e )
				{
					e.printStackTrace( ) ;
					user.setMessageError( e.getMessage( ) ) ;
				}
				
				tareas = MasterCardDAO.getAllActiveFormasInFlow( cveFlujo ) ;
				
				request.setAttribute( "Tareas" , tareas ) ;
				
				TareasOperationForm tof = new TareasOperationForm( ) ;
				
				tof.setCveFlujo( cveFlujo ) ;
				
				flujo = MasterCardDAO.findFlujoById( cveFlujo ) ;
				tof.setNombreFlujo( flujo.getNombre( ) ) ;
				
				request.setAttribute( "TareasOperationForm" , tof ) ;
				
                                MasterCardDAO.closeEntityManager( ) ;
				return mapping.findForward( "AddTareaIndex" ) ;
				
			case 16 :
				
				System.out.println( "TareasOperationIn (16)" ) ;
				
				System.out.println( "/FlujosFIndexIn.do?cveFlujo=" + cveFlujo + "&operation=15" ) ;
				user.setNextWorkArea( "/FlujosFIndexIn.do?cveFlujo=" + cveFlujo + "&operation=15" ) ;
				
                                MasterCardDAO.closeEntityManager( ) ;
				return mapping.findForward( "success" ) ;
				
			case 17 :
				
				System.out.println( "TareasOperationIn (17)" ) ;
				
				flujo = MasterCardDAO.findFlujoById( cveFlujo ) ;
				f.setNombreFlujo( flujo.getNombre( ) ) ;
				
				// sube las tareas
				tareas = MasterCardDAO.getAllActiveTareasWithFlow( cveFlujo ) ;
				
				// las sube al request
				request.setAttribute( "Tareas" , tareas ) ;
				request.setAttribute( "TareasOperationForm" , f ) ;
				
				
//				System.out.println( "/TareasIndexIn.do?cveFlujo=" + cveFlujo ) ;
//				user.setNextWorkArea( "/WEB-INF/pages/WorkArea/TareasIndex.jsp" ) ;
//				
                                MasterCardDAO.closeEntityManager( ) ;
				return mapping.findForward( "TareasIndex" ) ;
				
			case 18 :
				
				System.out.println( "TareasOperationIn: 15" ) ;
				
				tareas = MasterCardDAO.getAllActiveFormasInFlow( cveFlujo ) ;
				
				request.setAttribute( "Tareas" , tareas ) ;
				
				tof = new TareasOperationForm( ) ;
				
				tof.setCveFlujo( cveFlujo ) ;
				
				flujo = MasterCardDAO.findFlujoById( cveFlujo ) ;
				tof.setNombreFlujo( flujo.getNombre( ) ) ;
				
				request.setAttribute( "TareasOperationForm" , tof ) ;
				
                                MasterCardDAO.closeEntityManager( ) ;
				return mapping.findForward( "AddTareaIndex" ) ;
				
				
				
			// cualquier otro caso
			default :
		}
		
                MasterCardDAO.closeEntityManager( ) ;
		return mapping.findForward( "success" ) ;
	}
	
}
